package com.jsbj.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.tools.RequestHeaderUtils;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysRolePermission;
import com.jsbj.user.mapper.SysRolePermissionMapper;
import com.jsbj.user.service.ISysRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色-菜单信息表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-03-07
 */
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {

    @Autowired
    private HttpServletRequest request;

    @Override
    public Integer add(String inParam) {
        String userId = RequestHeaderUtils.getUserId(request);
        JSONObject object = JSONObject.parseObject(inParam);
        String permissionIds = object.getString("permissionIds");
        String roleId = object.getString("roleId");
        if (StringUtils.isNotEmpty(permissionIds)) {
            List<String> permissionList = Arrays.asList(permissionIds.split(","));
            for (String permissionId : permissionList) {
                SysRolePermission sysRolePermission = new SysRolePermission();
                sysRolePermission.setSysTenantPermissionId(permissionId);
                sysRolePermission.setRoleId(roleId);
                sysRolePermission.setCreaterId(userId);
                sysRolePermission.setCreateTime(new Date());
                sysRolePermission.setUpdateId(userId);
                sysRolePermission.setUpdateTime(new Date());
                baseMapper.insert(sysRolePermission);
            }
        }
        String ids = object.getString("ids");
        if (StringUtils.isNotEmpty(ids)) {
            List<String> notChooseIds = Arrays.asList(ids.split(","));
            for (String id : notChooseIds) {
                SysRolePermission sysRolePermission = baseMapper.selectById(id);
                sysRolePermission.setDisabled(1);
                sysRolePermission.setUpdateId(userId);
                sysRolePermission.setUpdateTime(new Date());
                baseMapper.updateById(sysRolePermission);
            }
        }
        return 1;
    }

    @Override
    public List<SysRolePermission> detail(String id) {
        LambdaQueryWrapper<SysRolePermission> qw = new LambdaQueryWrapper<>();
        qw.eq(SysRolePermission::getRoleId, id);
        qw.eq(SysRolePermission::getDisabled, 0);
        List<SysRolePermission> sysRolePermissions = baseMapper.selectList(qw);
        return sysRolePermissions;
    }

    @Override
    public void insertData(String tenantPermissionId, String roleId, String userId) {
        LambdaQueryWrapper<SysRolePermission> qw = new LambdaQueryWrapper<>();
        qw.eq(SysRolePermission::getRoleId, roleId);
        qw.eq(SysRolePermission::getSysTenantPermissionId, tenantPermissionId);
        qw.eq(SysRolePermission::getDisabled, 0);
        List<SysRolePermission> sysRolePermissions = baseMapper.selectList(qw);
        if (CollectionUtils.isEmpty(sysRolePermissions)) {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setSysTenantPermissionId(tenantPermissionId);
            sysRolePermission.setRoleId(roleId);
            sysRolePermission.setCreaterId(userId);
            sysRolePermission.setCreateTime(new Date());
            sysRolePermission.setUpdateId(userId);
            sysRolePermission.setUpdateTime(new Date());
            sysRolePermission.setDisabled(0);
            baseMapper.insert(sysRolePermission);
        }
    }

    @Override
    public void batchInsertData(String permissionsIds, String roleId, String userId, String tenantCode) {
        String[] permissionsIdArr = permissionsIds.split(",");
        List<String> permissionsIdList = Arrays.asList(permissionsIdArr);
        LambdaUpdateWrapper<SysRolePermission> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysRolePermission::getDisabled, 0);
        updateWrapper.eq(SysRolePermission::getRoleId, roleId);
        updateWrapper.notIn(SysRolePermission::getSysTenantPermissionId, permissionsIdList);
        updateWrapper.inSql(SysRolePermission::getSysTenantPermissionId, "select id from sys_tenant_permission where disabled = 0 and tenant_code='"+tenantCode+"'");

        SysRolePermission sysRolePermissionU = new SysRolePermission();
        sysRolePermissionU.setDisabled(1);
        sysRolePermissionU.setUpdateId(userId);
        sysRolePermissionU.setUpdateTime(new Date());

        baseMapper.update(sysRolePermissionU, updateWrapper);

        // 插入
        for (String permissionsId : permissionsIdList) {
            LambdaQueryWrapper<SysRolePermission> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysRolePermission::getDisabled, 0);
            queryWrapper.eq(SysRolePermission::getRoleId, roleId);
            queryWrapper.eq(SysRolePermission::getSysTenantPermissionId, permissionsId);
            queryWrapper.last(" limit 1");
            if (null == baseMapper.selectOne(queryWrapper)) {
                SysRolePermission sysRolePermission = new SysRolePermission();
                sysRolePermission.setSysTenantPermissionId(permissionsId);
                sysRolePermission.setRoleId(roleId);
                sysRolePermission.setCreaterId(userId);
                sysRolePermission.setCreateTime(new Date());
                sysRolePermission.setUpdateId(userId);
                sysRolePermission.setUpdateTime(new Date());
                baseMapper.insert(sysRolePermission);
            }
        }
    }
}
